//
//  LSBFileListCell.m
//  LZZSandboxViewer
//
//  Created by luzz on 2021/11/2.
//

#import "LSBFileListCell.h"
#import "LSBFileModel.h"
#import "LSBHelper.h"
@interface LSBFileListCell()
@property(nonatomic,strong)UIImageView * icon;
@property(nonatomic,strong)UILabel * fileNameLabel;
@property(nonatomic,strong)UILabel * fileDetailLabel;

@end

@implementation LSBFileListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}
-(void)setupUI
{
    self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    self.icon = [[UIImageView alloc] init];
    self.icon.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:self.icon];
    
    self.fileNameLabel = [[UILabel alloc] init];
    self.fileNameLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15];
    self.fileNameLabel.textColor = [UIColor darkTextColor];
    [self.contentView addSubview:self.fileNameLabel];
    
    self.fileDetailLabel = [[UILabel alloc] init];
    self.fileDetailLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
    self.fileDetailLabel.textColor = [UIColor grayColor];
    [self.contentView addSubview:self.fileDetailLabel];
    
}

CGFloat adapt_w(CGFloat x){
    return x * [UIScreen mainScreen].bounds.size.width/375.0;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat margin = 15;
    CGFloat icon_w = adapt_w(40);
    self.icon.frame = CGRectMake(margin, (self.contentView.bounds.size.height - icon_w)/2, icon_w, icon_w);
    CGFloat t_left = margin+ icon_w+margin;
    CGFloat t_w = self.contentView.bounds.size.width - 30 - t_left;
    CGSize nameSize = [self.fileNameLabel sizeThatFits:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)];
    if (self.fileDetailLabel.text.length > 0) {
        CGSize detailSize = [self.fileDetailLabel sizeThatFits:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)];
        CGFloat t_h = nameSize.height + 8 + detailSize.height;
        self.fileNameLabel.frame = CGRectMake(t_left, (self.contentView.bounds.size.height - t_h)/2, t_w, nameSize.height);
        self.fileDetailLabel.frame = CGRectMake(t_left, self.contentView.bounds.size.height - (self.contentView.bounds.size.height - t_h)/2 - detailSize.height, detailSize.width, detailSize.height);
    }else{
        
        self.fileNameLabel.frame = CGRectMake(t_left, (self.contentView.bounds.size.height - nameSize.height)/2, t_w, nameSize.height);
        
    }
    
}


- (void)setModel:(LSBFileModel *)model
{
    _model = model;
    
    self.fileNameLabel.text = model.name;
    if (model.fileType == LSBFileTypeDirectory) {
        self.icon.image = [LSBHelper imageWithName:@"folder"];
        self.fileDetailLabel.text = [NSString stringWithFormat:@"%@：%@ - %@ - %ld项",model.fileTypeName,model.creatTime,model.fileSize,(long)model.fileCount];
    }else{
        self.icon.image = [LSBHelper imageWithName:model.fileDes.iconName];
        self.fileDetailLabel.text = [NSString stringWithFormat:@"%@：%@ - %@",model.fileDes.fileTypeName,model.creatTime,model.fileSize];
    }
}

@end
